package com.ruoyi.jpjstb.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.jpjstb.mapper.ProjectMapper;
import com.ruoyi.jpjstb.mapper.param.ParamMapper;
import com.ruoyi.jpjstb.service.ProjectService;
import com.ruoyi.jpjstb.util.ExcelUtils;
import com.ruoyi.jpjstb.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ParamMapper paramMapper;

    @Override
    public List<Map<String, Object>> findProjectList(Map<String, Object> map) {
        return projectMapper.findProjectList(map);
    }

    @Override
    public int savePorject(Map<String, Object> map) {
        int count = 0;
        if (map.get("id") != null && !map.get("id").equals("")) {
            count = projectMapper.updatePorject(map);
            if (count > 0) {
                count = projectMapper.updateProInformation(map);
            }
        } else {
            String proId = StringUtil.getUUID();
            String informationId = StringUtil.getUUID();
            map.put("id", proId);
            count = projectMapper.addPorject(map);
            if (count > 0) {
                map.put("id", informationId);
                map.put("proId", proId);
                count = projectMapper.addProInformation(map);
            }
        }
        return count;
    }

    @Override
    public Map<String, Object> findProject(String id) {
        Map<String, Object> map=projectMapper.findProject(id);
        boolean netwok = (int) map.get("network") == 1 ? true : false;
        boolean erp = (int) map.get("erp") == 1 ? true : false;
        boolean attendance = (int) map.get("attendance") == 1 ? true : false;
        boolean monitor = (int) map.get("monitor") == 1 ? true : false;
        boolean devops = (int) map.get("devops") == 1 ? true : false;
        map.put("netwok", netwok);
        map.put("erp", erp);
        map.put("attendance", attendance);
        map.put("monitor", monitor);
        map.put("devops", devops);
        if(map.get("startTime")==null){
            map.put("startTime","");
        }
        if(map.get("endTime")==null){
            map.put("endTime","");
        }

        return map;
    }

    @Override
    public int removePorject(String ids) {
        int count = 0;
        String[] arr = ids.split(",");
        for (String id : arr) {
            count = projectMapper.removePorject(id);
            if (count > 0) {
                count = projectMapper.removeProInformation(id);
            } else {
                return count;
            }
        }
        return count;
    }

    @Override
    public List<Map<String, Object>> getProView() {
        return projectMapper.getProView();
    }

    @Override
    public Map<String, Object> getCount(String belongComp) {
        return projectMapper.getCount(belongComp);
    }

    @Override
    public AjaxResult export(Map<String, Object> map) {
        List<Map<String, Object>> list = projectMapper.findProjectList(map);
        List<Map<String, String>> colNameList = paramMapper.getColumns("fi_project_view");
        List<String> colList = new ArrayList<String>();
        for (Map<String, String> colMap : colNameList) {
            colList.add(colMap.get("colName"));
        }
        return ExcelUtils.exportData(colList, list, "项目信息");
    }
}
